package com.example.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.domain.*;
import com.example.mapper.OrdersMapper;
import com.example.service.*;
import com.example.service.ex.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

//订单的业务处理
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAddressBookService addressBookService;
    @Autowired
    private IShoppingCartService shoppingCartService;
    @Autowired
    private IOrderDetailService orderDetailService;
//    addressBookId	"1597867448373006338"
//    payMethod	1

    //添加订单信息
    @Override
    public void submit(Orders orders,Long userId) {
        //根据用户id查询用户的相关信息
        User user = userService.getById(userId);
        //跟据传递的地址id查询出订单的地址信息
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.defalt(user.getId());
        if (addressBook==null){
            throw new CustomException("用户地址不存在");
        }
        //根据用户id查询出购物车数据
        List<ShoppingCart> list = shoppingCartService.list(user.getId());
        if (list==null||list.size()==0){
            throw new CustomException("您的购物车数据为空");
        }
        //生成订单号
        long number = IdWorker.getId();
        //计算结账时的总金额
        AtomicInteger atomicInteger = new AtomicInteger(0);
        //遍历购物车中商品数据并算出总金额
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart:list){
            //创建一个订单明细对象
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(number);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetailList.add(orderDetail);
            //计算出总价格
            atomicInteger.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
        }
        orderDetailService.save(orderDetailList);
        //向订单对象中添加数据
        //订单号
        orders.setNumber(String.valueOf(number));
        //订单状态
        orders.setStatus(2);
        //设置userId
        orders.setUserId(user.getId());
        //设置地址id
        orders.setAddressBookId(addressBookId);
        //设置下单时间
        orders.setOrderTime(LocalDateTime.now());
        //设置结账时间
        orders.setCheckoutTime(LocalDateTime.now());
        //设置金额
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        //设置手机号
        orders.setPhone(user.getPhone());
        //设置地址
        orders.setAddress(addressBook.getDetail());
        //设置用户名
        orders.setUserName(user.getName());
        //设置收货人姓名
        orders.setConsignee(addressBook.getConsignee());
        ordersMapper.insert(orders);
    }
}
